﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OptikinetiqControls
{
    public class DrawPolygon
    {
        /// <summary>
        /// 路径
        /// </summary>
        public List<PointF> polygonPath = new List<PointF>();

        private Color _selectColor;
        public Color selectColor
        {
            get { return _selectColor; }
        }

        private Color _penColor;
        public Color penColor
        {
            get { return _penColor; }
            set
            {
                _penColor = value;

                int r = 255 - value.R;
                int g = 255 - value.G;
                int b = 255 - value.B;
                _selectColor = Color.FromArgb(r, g, b);
            }
        }

        private float _penSize;

        public float penSize
        {
            get { return _penSize; }
            set { _penSize = value; }
        }

        public bool isSelect;

        public void ChangeAllPath(PointF imagePoint) {
            float x = imagePoint.X - this.GetCenter().X;
            float y = imagePoint.Y - this.GetCenter().Y;

            for (int i = 0; i < polygonPath.Count; i++) {
                polygonPath[i] = new PointF(polygonPath[i].X + x, polygonPath[i].Y + y);
            }
        }


        public PointF GetCenter() {
            float minX = polygonPath.Min(p => p.X);
            float maxX = polygonPath.Max(p => p.X);
            float minY = polygonPath.Min(p => p.Y);
            float maxY = polygonPath.Max(p => p.Y);

            return new PointF(minX + (maxX - minX) / 2, minY + (maxY - minY) / 2);
        }

        public double GetArea() {
            int n = polygonPath.Count;
            if (n < 3)
            {
                return 0;
            }

            double area = 0;
            for (int i = 0; i < n; i++)
            {
                int j = (i + 1) % n;
                area += polygonPath[i].X * polygonPath[j].Y - polygonPath[j].X * polygonPath[i].Y;
            }

            return Math.Abs(area) / 2;
        }

        public float GetRadius() {
            //r = 宽高最大值/2
            float w = GetWidth();
            float h = GetHeight();

            return Math.Min(w, h);
        }

        public float GetWidth() {
            float minX = polygonPath.Min(p => p.X);
            float maxX = polygonPath.Max(p => p.X);
            return maxX - minX;
        }

        public float GetHeight() {
            float minY = polygonPath.Min(p => p.Y);
            float maxY = polygonPath.Max(p => p.Y);

            return maxY - minY;
        }

        public float GetCenterSize() {
            return GetRadius() * 2 / 10;
        }
    }
}
